package exportPDF;

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.util.Vector;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;

import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;

import entityJPA.Candidate;
import entityJPA.Position;
import entityJPA.Positiondescription;

@Stateless
@LocalBean
public class Export {
	private Document document;
	private ByteArrayOutputStream baos;

	public byte[] exportPosition(Position position) {

		document = new Document();
		baos = new ByteArrayOutputStream();
		try {
			exportPos(position);
			document.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return baos.toByteArray();
	}

	public byte[] exportPositionAndCandidates(Position position,
			Vector<Candidate> candList) {
		try {
			exportPos(position);

			Paragraph p = new Paragraph("CANDIDATES");
			p.setAlignment(Element.ALIGN_CENTER);
			document.add(p);

			for (Candidate cand : candList) {
				document.add(new Paragraph(""));
				document.add(new Paragraph("NAME: " + cand.getFirstName() + " "
						+ cand.getLastName()));
			}
			document.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return baos.toByteArray();
	}

	private void exportPos(Position position) {

		document = new Document();
		baos = new ByteArrayOutputStream();
		try {

			PdfWriter.getInstance(document, baos);
			document.open();
			Paragraph p = new Paragraph(position.getName() + " description");
			p.setAlignment(Element.ALIGN_CENTER);
			document.add(p);
			document.add(new Paragraph("Status: "
					+ position.getPositionstatus().getValue()));
			Positiondescription description = position.getPositiondescription();
			document.add(new Paragraph("Project: " + description.getProject()));
			document.add(new Paragraph("Department: "
					+ description.getDepartment().getName()));
			document.add(new Paragraph("Requirements: "
					+ description.getRequirements()));
			document.add(new Paragraph("Responsabilities: "
					+ description.getResponsabilities()));
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}
